package com.cloudatum.service;

import com.cloudatum.redis.RedisUtil;
import com.cloudatum.utils.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;

/**
 * 验证码服务
 * Created by zhoujian on 2015/12/11.
 */
@Service
public class VerificationCodeService {

    @Autowired
    private VerificationCodeMapper verificationCodeMapper;

    public Map<String, String> randOne() {
        return verificationCodeMapper.randOne();
    }

    public List<Map<String, String>> getAllVerification(){
        return verificationCodeMapper.getAllVerification();
    }

    public Map<String, String> randCacheOne() {
        Jedis jedis = null;
        Map<String, String> map = null;
        try {
            jedis = RedisUtil.getJedis();
            String id = jedis.srandmember(Constants.VERIFICATION_ENTITYID_SET);
            if (StringUtils.isNotEmpty(id)) {
                map = jedis.hgetAll(Constants.VERIFICATION_ENTITY + id);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (null != jedis){
                RedisUtil.returnResource(jedis);
            }
        }
        return map;
    }


}
